﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClientAPI
{
    public class Protocol
    {
        private static Protocol protocolInstance = null;
        private bool loggedIn = false;
        private bool replied = false;
        private string userName;
        private string message;
        Chat chatProtocol = new Chat();

        // singleton instance of the protocol

        public static Protocol Instance
        {
            get
            {
                if (protocolInstance == null)
                    protocolInstance = new Protocol();
                return protocolInstance;
            }
        }

        // returns true if loggin
      
        public bool IsLoggedIn()
        {
            return loggedIn;
        }

        // sends the login request
        //$FRANK,USER,LOGIN,username,password,

        public void Login(string userName, string password)
        {
            message = "$FRANK,USER,LOGIN," + userName + "," + password + ",";
            Network.Instance.SendMessage(message);
            Instance.userName = userName;
        }

        //sends the upload request
        //$FRANK,FILE,UPLOAD,filename,description,useruploading,

        public void SendFile(string filePath, string fileName, string description)
        {
            message = "$FRANK,FILE,UPLOAD,"+fileName+","+description+","+Instance.userName+",";
            Network.Instance.SendFile(message, filePath, fileName);
        }

        //sends create user request
        //$FRANK,USER,CREATE,username,firstname,lastname,password

        public void CreateUser(string username, string fname, string lname, string pass, string phoneNum, string email)
        {
            message = "$FRANK,USER,CREATE," + username + "," + fname + "," + lname + "," + pass + "," + phoneNum + "," + email + ",";
            Network.Instance.SendMessage(message);
        }

        //sends disconnect request and clears username in cache
        //$FRANK,CLIENT,DISCONNECT,username,

        public void Disconnect()
        {
            message = "$FRANK,CLIENT,DISCONNECT," + userName + ",";
            Network.Instance.SendMessage(message);
            Instance.userName = null;
        }

        public void Download(string fileName)
        {
            message = "$FRANK,FILE,DOWNLOAD," + fileName + ",";
            Network.Instance.SendMessage(message);
        }

        public void DownloadStarted(string fileName)
        {
            message = "$FRANK,FILE,DOWNLOAD,STARTED," + userName+ "," + fileName + ",";
            Network.Instance.SendMessage(message);
        }

        public void DownloadStopped(string fileName)
        {
            message = "$FRANK,FILE,DOWNLOAD,STOPPED," + userName + "," + fileName + ",";
            Network.Instance.SendMessage(message);
        }
        
        public void SendChat(string messageReceiver, string chatMessage)
        {
            chatProtocol.ChatFunction(messageReceiver, (userName + ": " + chatMessage));
            message = "$FRANK,CHAT," + userName + "," + messageReceiver + "," + chatMessage + ",";
            Network.Instance.SendMessage(message);
        }

        public string GetChatLog(string messageSender)
        {
            string chatLog = chatProtocol.getLog(messageSender);
            return chatLog;
        }

        public void SneakyHi()
        {
            chatProtocol.ChatFunction("admin", "Hi!!!");
        }

        public bool WaitForLoginReply()
        {
            while (!Instance.replied)
            {
            }
            Instance.replied = false;
            return loggedIn;
            
        }

        public void MessageHandler(string message)
        {
            string[] messageArray = message.Split(',');
            if (messageArray[0] == "$FRANK")
            switch (messageArray[1])
            {
                case "CLIENT":
                    System.Diagnostics.Debug.WriteLine("Message recieved"+message);
                    break;
                // recieves user login details
                case "USER":
                    switch(messageArray[2])
                    {
                        case "OK":
                            Instance.loggedIn = true;
                            Instance.replied = true;
                            break;
                        case "BAD":
                            Instance.replied = true;
                            break;
                    }
                    break;
                // handles files sent and recieved
                case "FILE":
                    switch (messageArray[2])
                    {
                        case "DOWNLOAD":
                            switch (messageArray[3])
                            {
                                case "START":
                                    Network.Instance.FileThreadConnect();
                                    break;
                                case "END":
                                    Network.Instance.FileEnd();
                                    break;
                            }
                            break;
                        case "UPLOAD":
                            switch (messageArray[3])
                            {
                                case "START":
                                    //code
                                    break;
                                case "END":
                                    //code
                                    break;
                            }
                            break;
                    }
                    break;
                case "CHAT":
                    chatProtocol.ChatFunction(messageArray[2], messageArray[4]);
                    ClientAPI.Instance.ChatMessageReceived(messageArray[2]);
                    break;
                default:
                    System.Diagnostics.Debug.WriteLine("Invalid Message recieved: " + message);
                    break;
            }
            else System.Diagnostics.Debug.WriteLine("WE BEEN HACKED!!!");
        }
    }
}
